package com.leps.nq.bi;

import cn.hutool.core.convert.Convert;
import com.leps.nq.bi.mapper.CustBIMapper;
import com.leps.nq.bi.vo.AlarmStatVo;
import com.leps.nq.bi.vo.SimpleEntry;
import com.leps.nq.bi.vo.SummaryAndDetailsVo;
import com.leps.nq.bu.entity.BuArea;
import com.leps.nq.bu.entity.BuCust;
import com.leps.nq.bu.service.IBuAreaService;
import com.leps.nq.bu.service.IBuCustService;
import com.leps.nq.bu.service.IBuWellService;
import com.leps.nq.bu.vo.WellVo;
import com.leps.nq.common.util.SecurityUtil;
import com.leps.nq.common.vo.R;
import com.leps.nq.common.vo.RBuilder;
import com.leps.nq.op.entity.OpAlarm;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/bi-cust")
@Api(tags = "客户BI看板")
public class CustBIController {
    @Autowired
    CustBIMapper custBIMapper;
    @Autowired
    IBuWellService wellService;
    @Autowired
    IBuAreaService areaService;
    @Autowired
    IBuCustService custService;

    private int getCustId() {
        return this.custBIMapper.selectCustIdByCustUserId(SecurityUtil.getUserId());
    }

    @GetMapping("/current-cust-info")
    @ApiOperation("获取当前用户（客户用户）对应的客户信息")
    public R<BuCust> currentCustInfo() {
        int custId = this.getCustId();
        BuCust cust = this.custService.getById(custId);
        return RBuilder.ok(cust);
    }

    @GetMapping("/areas")
    @ApiOperation("获取该客户下属平台列表")
    public R<List<BuArea>> areasOfThisCust() {
        int custId = this.getCustId();
        List<BuArea> list = this.areaService.lambdaQuery().eq(BuArea::getCustId, custId).list();
        return RBuilder.ok(list);
    }

    @GetMapping("/wells-for-map")
    @ApiOperation("根据平台id查询井信息(不传参则查询该客户前10个井)")
    public R<List<WellVo>> wellsForMap(Integer stationId) {
        int custId = this.getCustId();
        List<WellVo> list = this.wellService.getWellsByCustIdAndStationId(custId, stationId);
        return RBuilder.ok(list);
    }

    @ApiOperation("区块服务井数")
    @GetMapping("/well")
    public R<List<SimpleEntry<Long>>> wellNumbersOfEveryArea() {
        int custId = this.getCustId();
        List<SimpleEntry<Long>> list = this.custBIMapper.selectWellNumbersByCustGroupByArea(custId);
        return RBuilder.ok(list);
    }

    @ApiOperation("年药剂用量统计")
    @GetMapping("/year-replenish")
    public R<SummaryAndDetailsVo> yearReplenish() {
        int custId = this.getCustId();
        List<SimpleEntry<Double>> list = this.custBIMapper.selectYearDosageByCustGroupByArea(custId);
        double summary = list.stream().mapToDouble(item -> item.getValue()).sum();
        SummaryAndDetailsVo vo = new SummaryAndDetailsVo();
        vo.setDetails(list);
        vo.setSummary(summary);
        return RBuilder.ok(vo);
    }

    @ApiOperation("月药剂用量统计")
    @GetMapping("/month-replenish")
    public R<SummaryAndDetailsVo> monthReplenish() {
        int custId = this.getCustId();
        List<SimpleEntry<Double>> list = this.custBIMapper.selectMonthDosageByCustGroupByArea(custId);
        double summary = list.stream().mapToDouble(item -> item.getValue()).sum();
        SummaryAndDetailsVo vo = new SummaryAndDetailsVo();
        vo.setDetails(list);
        vo.setSummary(summary);
        return RBuilder.ok(vo);
    }

    @ApiOperation("日药剂量统计")
    @GetMapping("/today-replenish")
    public R<SummaryAndDetailsVo> todayReplenish() {
        int custId = this.getCustId();
        List<SimpleEntry<Double>> list = this.custBIMapper.selectTodayDosageByCustGroupByArea(custId);
        double summary = list.stream().mapToDouble(item -> item.getValue()).sum();
        SummaryAndDetailsVo vo = new SummaryAndDetailsVo();
        vo.setDetails(list);
        vo.setSummary(summary);
        return RBuilder.ok(vo);
    }

    @ApiOperation("区块管线最长保护天数")
    @GetMapping("/protect-day")
    public R<SimpleEntry<Integer>> protectDay() {
        int custId = this.getCustId();
        SimpleEntry<Integer> entry = this.custBIMapper.selectProtectDayByCustId(custId);
        return RBuilder.ok(entry);
    }

    @ApiOperation("泵注设备数目")
    @GetMapping("/pump-number")
    public R<List<SimpleEntry<Long>>> pumpNumber() {
        int custId = this.getCustId();
        List<SimpleEntry<Long>> list = this.custBIMapper.countPumpNumberByCustGroupByOnlineStatus(custId);
        return RBuilder.ok(list);
    }

    @ApiOperation("报警日志")
    @GetMapping("/alarm")
    public R<AlarmStatVo> statAlarm() {
        int custId = this.getCustId();
        List<Map> list = this.custBIMapper.countAlarmByCustGroupByAlarmType(custId);
        List<OpAlarm> items = this.custBIMapper.selectUnhandledAlarmsByCustId(custId);
        AlarmStatVo vo = new AlarmStatVo();
        vo.setItems(items);
        vo.setLiquidLevel(Convert.toLong(list.get(0).get("value")));
        vo.setPressure(Convert.toLong(list.get(1).get("value")));
        vo.setFault(Convert.toLong(list.get(2).get("value")));
        vo.setBrokenNetwork(Convert.toLong(list.get(3).get("value")));
        return RBuilder.ok(vo);
    }
}
